package com.dgut.lotterySystem.controller;

import com.dgut.lotterySystem.common.pojo.CommonResult;
import com.dgut.lotterySystem.common.utils.JacksonUtil;
import com.dgut.lotterySystem.controller.param.UserMessageLoginParam;
import com.dgut.lotterySystem.controller.param.UserPasswordLoginParam;
import com.dgut.lotterySystem.controller.param.UserRegisterParam;
import com.dgut.lotterySystem.controller.result.UserBaseInfoResult;
import com.dgut.lotterySystem.controller.result.UserLoginResult;
import com.dgut.lotterySystem.controller.result.UserRegisterResult;
import com.dgut.lotterySystem.service.dto.UserInfoDTO;
import com.dgut.lotterySystem.service.dto.UserLoginDTO;
import com.dgut.lotterySystem.service.enums.IdentityEnum;
import com.dgut.lotterySystem.service.impl.UserServiceImpl;
import com.dgut.lotterySystem.service.dto.UserRegisterDTO;
import com.dgut.lotterySystem.service.impl.VerificationCodeServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/user")
public class UserController {

    private  final static Logger logger= (Logger) LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private VerificationCodeServiceImpl verificationCodeService;
    @PostMapping("/register")
    public CommonResult<UserRegisterResult> userRegister(@Validated @RequestBody UserRegisterParam param){
        logger.info("userRegister UserRegisterParam"+ JacksonUtil.writeValueAsString(param));
        UserRegisterDTO userRegisterDTO=userService.register(param);
        return CommonResult.success(convertToUserRegisterResult(userRegisterDTO));
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    @PostMapping("/verification-code/send")
    public CommonResult<Boolean> sendVerificationCode(String phoneNumber){
        logger.info("sendVerificationCode phoneNumber"+phoneNumber);
        verificationCodeService.setVerificationCode(phoneNumber);
        return CommonResult.success(Boolean.TRUE);
    }

    /**
     * 使用短信验证码登录
     * @param param
     * @return
     */
    @PostMapping("/message/login")
    public CommonResult<UserLoginResult> messageLogin(@Validated @RequestBody UserMessageLoginParam param){
        logger.info("messageLogin param"+param);
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }

    /**
     *  使用密码登录，可以是邮箱，可以是手机号
     * @param param
     * @return
     */
    @PostMapping("/password/login")
    public CommonResult<UserLoginResult> passwordLogin(@Validated @RequestBody UserPasswordLoginParam param){
        logger.info("messageLogin param"+param);
        UserLoginDTO userLoginDTO=userService.login(param);
        return CommonResult.success(convertToUserLoginResult(userLoginDTO));
    }
    @GetMapping("/getUserInfoList")
    public CommonResult<List<UserBaseInfoResult>> getUserInfoList(String identity){
        logger.info("getUserInfoList param"+identity);
        List<UserInfoDTO> userList=userService.getUserInfoList(IdentityEnum.forName(identity));
        return CommonResult.success(convertToUserInfoList(userList));
    }

    private List<UserBaseInfoResult> convertToUserInfoList(List<UserInfoDTO> userList) {
        if(CollectionUtils.isEmpty(userList)){
            return Arrays.asList();
        }
        List<UserBaseInfoResult> list=userList.stream().map(userInfoDTO -> {
            UserBaseInfoResult userBaseInfoResult=new UserBaseInfoResult();
            userBaseInfoResult.setUserId(userInfoDTO.getId());
            userBaseInfoResult.setUserName(userInfoDTO.getUserName());
            userBaseInfoResult.setIdentity(userInfoDTO.getIdentity());
            return userBaseInfoResult;
        }).collect(Collectors.toList());
        return list;
    }

    private UserLoginResult convertToUserLoginResult(UserLoginDTO userLoginDTO) {
        UserLoginResult userLoginResult=new UserLoginResult();
        userLoginResult.setToken(userLoginDTO.getToken());
        userLoginResult.setMandatoryIdentity(userLoginDTO.getMandatoryIdentity().name());
        return userLoginResult;
    }

    private UserRegisterResult convertToUserRegisterResult(UserRegisterDTO userRegisterDTO){
        UserRegisterResult userRegisterResult=new UserRegisterResult();
        userRegisterResult.setUserId(userRegisterDTO.getUserId());
        return userRegisterResult;
    }
}
